from Web.Tools import Tool
import cgi

def jiexiToHtml(s):
    #A
    linkList= Tool.text_getMiddle( s , "a(" , ")" )
    linkListAll = Tool.text_getMiddle2( s , "a(" , "]" )
    for u,link in enumerate(linkList):
        s=s.replace(linkListAll[u],"<a href='"+link+"'>"+link+"</a>")

    #IMG
    linkList= Tool.text_getMiddle( s , "img[" , "]" )
    linkListAll = Tool.text_getMiddle2( s , "img[" , "]" )
    for u,link in enumerate(linkList):
        s=s.replace(linkListAll[u],"<img src='"+link+"'>")

    #PRE
    linkList= Tool.text_getMiddle( s , "[pre]" , "[/pre]" )
    linkListAll = Tool.text_getMiddle2( s , "[pre]" , "[/pre]" )
    for u,link in enumerate(linkList):
        #print(link,1111)
        s=link.replace("<br>","换行符2")
        s = s.replace( "<br />" , "换行符1" )
        #print(s)
        s=s.replace(s,"<pre>"+cgi.escape(s)+"</pre>")
        #print( s )
        s = s.replace( "换行符2" ,"<br>" )
        s = s.replace( "换行符1", "<br />"  )
        #print( s )
    s = s.replace( "\n" , "<br>" )
    s = s.replace( "[hr]" , "<hr>" )
    s = s.replace( "'" , "\'" )

    #"'
    '''
    s = s.replace( "('" , "#LEFTKUOHAO#" )
    s = s.replace( "')" , "#RIGHTKUOHAO#" )
    s = s.replace( "','" , "#DOUHAO#" )
    s = s.replace( "\'" , "\\'" )
    s = s.replace("#LEFTKUOHAO#", "('"  )
    s = s.replace("#RIGHTKUOHAO#"  ,  "')")
    s = s.replace(  "#DOUHAO#","','"  )
    '''
    return s


def requestParamToInstance(instance,requestinstance):
    #post值赋予对象中
    for key in requestinstance.keys( ):
        print( key , jiexiToHtml( requestinstance[ key ] ) )
        if hasattr(instance,key):
            setattr(instance,key,jiexiToHtml(requestinstance[key]))

    return instance

def argToString(arg):
    str="?"
    for (k,v) in arg.items():
        str+=k+"="+v+"&"
    return str.strip("&")